package com.right.handler;

import com.right.command.CsCommand;
import com.right.common.ResponseData;
import com.right.controller.TestController;
import com.right.util.SpringUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Created by ding on 2019/1/24.
 */
@Slf4j
public class LockStatusHandler implements  PackageHandler{
    @Override
    public boolean applies(List<String> dataList) {
        return CsCommand.LOCK_STATE.equals(dataList.get(1));
    }

    @Override
    public boolean handleMessage(ChannelHandlerContext ctx, List<String> dataList) {
        log.info("获取锁状态");
        String lockNo = dataList.get(0);
        TestController testController = SpringUtil.getBean(TestController.class);
        try {
            ResponseData re = testController.getLockStatus(lockNo);
            if (re.getCode()==1){
                List<String> d = (List<String>) re.getData();
                if (d!=null){//201,1
                    ctx.channel().writeAndFlush(d.get(d.size() - 1));
                }
                return false;
            }
            ctx.channel().writeAndFlush(re.getMsg());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }
}
